<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/7/23
 * Time: 17:41
 */
namespace app\xueao\controller;

use controller\XueaoAdmin;
use service\DataService;
use service\MemberService;
use think\Db;
use think\Exception;

class Member extends XueaoAdmin
{
    public $table='store_member';
    /**
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->title = '用户管理';
        $get = $this->request->get();
        $db = Db::name($this->table);
        $type=input('type/d',1);
        $db->where('type', $type);
        if (isset($get['nickname']) && $get['nickname'] !== '') {
            $db->whereLike('nickname|name', "%{$get['nickname']}%");
        }
        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($start, $end) = explode(' - ', $get['create_at']);
            $db->whereBetween('create_at', [strtotime("{$start} 00:00:00"), strtotime("{$end} 23:59:59")]);
        }
        if($type==1){
            $this->_assign_data();
        }
        return parent::_list($db->order('id desc'));
    }

    /**
     * @param $data
     */
    public function _form_filter(&$data,$extend_data)
    {
        if($this->request->isGet()){
            if($this->request->get('id')){
                $this->title='编辑用户';
            }else{
                $this->title='添加用户';
            }
            $this->_assign_data();
        }else{
            if($data['id']){
                $validate=new \app\xueao\validate\Member();
                if(false===$validate->check($data)){
                    $this->error($validate->getError());
                }
            }
        }
    }

    private function _assign_data(){
        //获取所有客户经理
        $managers=Db::name('store_member')
            ->where('status',1)
            ->where('type',MemberService::MEMBER_TYPE_SUPPLIER)
            ->field('id,name')->select();
        $this->assign('managers',$managers);
    }

    /**
     * 设为经销商
     * @return mixed
     */
    public function set_supplier()
    {
        if($this->request->isPost()){
            $post=$this->request->only(['id','name','point'],'post');
            $validate=new \app\xueao\validate\Member();
            if(false===$validate->scene('becomesupplier')->check($post)){
                $this->error($validate->getError());
            }
            Db::startTrans();
            try{
                $remark='您于['.date('Y/m/d H:i:s').']升级成为经销商,平台为您充值['.$post['point'].']积分';
                MemberService::log_account_change($post['id'],$post['point'],MemberService::ACCOUNT_LOG_TYPE_PARENT,$remark);
                unset($post['point']);
                $post['type']=MemberService::MEMBER_TYPE_SUPPLIER;
                $post['parent_id']=0;
                Db::name('store_member')->update($post);
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
            Db::commit();
            $this->success('设置成功','');
        }else{
            return $this->fetch();
        }
    }

    /**
     * 添加/编辑成功回跳处理
     * @param bool $result
     */
    protected function _form_result($result,$data)
    {
        if ($result !== false) {
            list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url(request()->module().'/'.request()->controller().'/index')];
            $this->success('数据保存成功！', "{$base}#{$url}?spm={$spm}&type=".$data['type']);
        }
    }

    /**
     * 改变积分
     * @return mixed
     * @throws Exception
     */
    public function change_point()
    {
        if($this->request->isPost()){
            $post=$this->request->only(['id','point'],'post');
            $validate=new \app\xueao\validate\Member();
            if(false===$validate->scene('changepoint')->check($post)){
                $this->error($validate->getError());
            }
            $remark='平台于['.date('Y/m/d H:i:s').']为您充值['.$post['point'].']积分';
            MemberService::log_account_change($post['id'],$post['point'],MemberService::ACCOUNT_LOG_TYPE_PARENT,$remark);
            list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url(request()->module().'/'.request()->controller().'/index')];
            $this->success('改变成功','');
        }else{
            return $this->fetch();
        }
    }

}